﻿using System;
using agsXMPP;

public partial class _default : System.Web.UI.Page
{
    static protected XmppClientConnection Client = null;
    static protected string TargetJid = "2xhjafc3ep9am2ea66g1c3nvzr@public.talk.google.com";
    //3olnwp6g7p1ad34bf1owt5xzsg

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (_default.Client == null)
            {
                _default.Client = new XmppClientConnection("gmail.com");
                _default.Client.AutoResolveConnectServer = false;
                _default.Client.ConnectServer = "talk.google.com";
                _default.Client.Open("nlareu.boot.send", "nicolaslareu");

                //client.OnLogin += delegate(object o)
                //{
                //    this.SendBtn.Enabled = true;
                //    //this.log.InnerHtml = "Logged";
                //};
                //_default.Client.OnPresence += client_OnPresence;
                _default.Client.OnMessage += new agsXMPP.protocol.client.MessageHandler(xmpp_OnMessage);
            }
        }
        catch (Exception ex)
        {
            //this.log.InnerHtml += "/r/n" + ex.Message;
        }
    }

    protected void Send(string msg)
    {
        agsXMPP.protocol.client.Message newmsg = new agsXMPP.protocol.client.Message();
        newmsg.Type = agsXMPP.protocol.client.MessageType.chat;
        newmsg.To = new Jid(TargetJid);
        newmsg.Body = msg;

        _default.Client.Send(newmsg);
    }

    protected void client_OnPresence(object sender, agsXMPP.protocol.client.Presence pres)
    {
        agsXMPP.protocol.client.Message newmsg = new agsXMPP.protocol.client.Message();
        newmsg.Type = agsXMPP.protocol.client.MessageType.chat;
        newmsg.To = new Jid(pres.From.Bare);
        newmsg.Body = "test sap net";

        // Send response.
        _default.Client.Send(newmsg);
    }
    protected void xmpp_OnMessage(object sender, agsXMPP.protocol.client.Message msg)
    {
        if (msg.Chatstate == agsXMPP.protocol.extensions.chatstates.Chatstate.composing)
            return;

        // This is called for any message received.
        //this.log.InnerHtml += "/r/nReceived: " + msg.Body;

        //// Let our chat bot respond to the message.
        //if (msg.Body == null || msg.Body == "")
        //    return;

        //string response = "Not supported yet";
        //string msgFixed = msg.Body.Trim();
        //int blankCharIndex = msgFixed.IndexOf(" ");
        //string comHeader = "";
        //string comBody = "";

        //if (blankCharIndex == -1)
        //{
        //    comHeader = msgFixed.ToUpper();
        //}
        //else
        //{
        //    comHeader = msgFixed.Substring(0, blankCharIndex).ToUpper();
        //    comBody = msgFixed.Substring(blankCharIndex + 1, msgFixed.Length - blankCharIndex - 1);
        //}

        

        //// Create a new message.
        //agsXMPP.protocol.client.Message newmsg = new agsXMPP.protocol.client.Message();
        //newmsg.Type = agsXMPP.protocol.client.MessageType.chat;
        //newmsg.To = new Jid(msg.From.Bare);
        //newmsg.Body = response;

        //// Send response.
        //client.Send(newmsg);
    }
    protected void Button_Click(object sender, EventArgs e)
    {
        ButtonCommand.ButtonCommand btn = (ButtonCommand.ButtonCommand)sender;
        this.Send(btn.ChatCommand);
    }
}